<?php

/* ----------------------------------------------
  Caloris: Menus

  $Id$
  $Author$

  Navigace: Úprava
  ---------------------------------------------- */

require_once("../../../index.php");
Caloris\ACL::authenticate();

define('_MODULE_ACTIVE', 'menu');

$tidy_options = array(
    'input-xml' => true,
    'output-xml' => true,
    'indent' => true,
    'wrap' => false,
);

if ($_REQUEST["name"] == 'categoryDelete') {
    $doc = new DOMDOcument;
    $doc->load(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml');

    $xpath = new DOMXpath($doc);
    $qry = '//category[@id="' . $_REQUEST["id"] . '"]';

    foreach ($xpath->query($qry) as $node) {
        $node->parentNode->removeChild($node);
    }

    $doc->save(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml');
    if (class_exists('tidy')) {
        $tidy = new tidy();
        $tidy->parseFile(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml', $tidy_options, 'utf8');
        $tidy->cleanRepair();

        $xml = simplexml_load_string($tidy);
        $xml->asXML(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml');
    }

    if ($_POST["j"] != 1) {
        header("location: " . $_SERVER["HTTP_REFERER"] . "");
        exit();
    } else {
        echo "{\"msg\": \"done\"}";
        exit();
    }
} elseif ($_REQUEST["name"] == 'categoryInsert') {
    $xml = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml');
    $xml->data->id = $xml->data->id + 1;
    $xml->asXML(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml');

    $doc = new DOMDOcument;
    $doc->load(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml');
    $xpath = new DOMXpath($doc);

    $qry = '//category[@id="' . $_REQUEST["id"] . '"]';

    foreach ($xpath->query($qry) as $node) {
        $foo = $doc->createElement('category');
        $node->appendChild($foo);

        $idAttribute = $doc->createAttribute("id");
        $foo->appendChild($idAttribute);

        $priceValue = $doc->createTextNode(($xml->data->id));
        $idAttribute->appendChild($priceValue);

        $bar = $doc->createElement("name", $_REQUEST["new"]);
        $foo->appendChild($bar);

        $bazz = $doc->createElement("link", "");
        $foo->appendChild($bazz);
    }

    $doc->save(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml');

    if (class_exists('tidy')) {
        $tidy = new tidy();
        $tidy->parseFile(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml', $tidy_options, 'utf8');
        $tidy->cleanRepair();

        $xmi = simplexml_load_string($tidy);
        $xmi->asXML(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml');
    }

    if ($_POST["j"] != 1) {
        header("location: " . $_SERVER["HTTP_REFERER"] . "");
        exit();
    } else {
        echo "{\"msg\": \"done\"}";
    }
} elseif ($_REQUEST["name"] == 'categoryEdit') {
    $doc = new DOMDOcument;
    $doc->load(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml');
    $xpath = new DOMXpath($doc);

    $qry = '//category[@id="' . $_REQUEST["id"] . '"]';

    $library = new SimpleXMLElement(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml', null, true);
    $book = $library->xpath($qry);
    $book[0]->name = $_REQUEST["title"];
    $book[0]->link = $_REQUEST["link"];
    $book[0]->description = $_REQUEST["description"];
    $library->asXML(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml');

    if ($_POST["j"] != 1) {
        header("location: " . $_SERVER["HTTP_REFERER"] . "");
        exit();
    } else {
        echo "{\"msg\": \"done\"}";
    }
}

$content = '
<h1>' . t('menu', 'menu') . '</h1>';

$id = $_REQUEST["id"];

if ($id != '') {
    $xpath = '//category[@id="' . $id . '"]';
} else {
    $xpath = '/core';
}

$xmlMenuF = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/categories.xml');
$result = $xmlMenuF->xpath($xpath);

while (list(, $node) = each($result)) {
    $content .= '
<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/' . _MODULE_ACTIVE . '/index.php" method="post" class="form-inline">
<input type="hidden" name="name" value="categoryInsert"  />
<input type="hidden" name="id" value="' . $id . '" />
<input type="text" name="new" />
<input type="submit" value="[cal:t(insert;menu) /]" class="btn btn-primary" />
</form>

<h2>[cal:t(setCategory;menu) /]</h2>

<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/' . _MODULE_ACTIVE . '/index.php" method="post" class="form-horizontal">
<input type="hidden" name="name" value="categoryEdit"  />
<input type="hidden" name="id" value="' . $id . '"  />

<div class="control-group">
<label class="control-label">[cal:t(name;menu) /]</label>
<div class="controls"><input type="text" name="title" value="' . Caloris\Text::caltag($node->name) . '" class="input-xlarge" /></div>
</div>

<div class="control-group">
<label class="control-label">[cal:t(description;menu) /]</label>
<div class="controls"><textarea name="description" class="input-xlarge">' . $node->description . '</textarea></div>
</div>

<div class="control-group">
<label class="control-label">[cal:t(link;menu) /]</label>
<div class="controls"><input type="text" name="link" value="' . $node->link . '" id="sitekey" class="input-xlarge" /></div>
</div>

<div class="control-group">
<div class="controls"><input type="submit" value="[cal:t(save;menu) /]" class="btn btn-primary" /></div>
</div>
</form>

<h2>[cal:t(subCategories;menu) /]</h2>

<table class="table table-striped">' . PHP_EOL;

    for ($a = 0; $a < count($node->category); $a++) {
        $content .= '
    <tr class="trbc_' . $a . '">
        <td style="width: 20px;">
<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/' . _MODULE_ACTIVE . '/index.php" id="formshw_' . $a . '" name="trbc_' . $a . '" method="post" onsubmit="return false;">
<input type="hidden" name="name" value="categoryDelete"  />
<input type="hidden" name="id" value="' . $node->category[$a]["id"] . '"  />
<input type="submit" value="Smazat" id="formx_' . $a . '" alt="x" class="btn btn-danger" />
</form>
        </td>
        <td><a href="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/' . _MODULE_ACTIVE . '/index.php?id=' . $node->category[$a]["id"] . '">' . $node->category[$a]->name . '</a></td>
    </tr>';
    }
}

$content .= '
</table>' . PHP_EOL;

// Suggestion for link field in Category settings

$xmaPages = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml');

$siteList = '';

for ($a = 0; $a < count($xmaPages->page); $a++) {
    $siteList .= '' . $xmaPages->page[$a]["id"] . ';';
}

$content .= '
<script type="text/javascript">
$(document).ready(function(){
var sitekey = "' . substr($siteList, 0, -1) . '".split(";");
$("input[id$=\'sitekey\']").autocomplete(sitekey);
});
</script>' . PHP_EOL;

$data["title"] = 'Menu';

$tmpl = new Caloris\Template;
$tmpl->input($content);
$tmpl->renderHead($data);
echo $tmpl->renderBody('caloris-admin');